//  配置
var _config = {
  //  base: 'http://t.imaisu.com',
  base: '',
  creatShortLink: '/api/link',
  /*
  url: /api/link
  body: {
          "phone": "15995578262",
          "id": 731,
          "hmpl": "20170901",
          "base_url": "MALL_6",
          "product":"CK312",
          "delivery_source": "FEI_MESSAGE"
  }
  */
  getProduct: '/api/product', //  查询货品
  getBaseUrl: '/api/baseurl', //  查询根域名
  getDeliverySource: '/api/delivery_source' //  查询投放来源
}

var _status = {
  0: '等待中',
  1: '生成中',
  2: '生成失败',
  3: '生成完毕',
}

//  业务逻辑
var _main = {
  data: {

  },
  //  点创建按钮
  submitCreateForm: function() {
    let _params = {
      shortlink: encodeURIComponent(document.querySelector('#ShortLink').value)
    }
    if (!_params.shortlink || _params.shortlink.length == 0 ){
      _params = { phone : document.querySelector('#phone').value }
    }
    //  前面这个bug是传武挖的 startdate, enddate, goods, source
    let _startdate = document.querySelector('#startdate').value,
    _enddate = document.querySelector('#enddate').value,
    _goods = this.getSelected(document.querySelector('#product')).text,
    _source = this.getSelected(document.querySelector('#delivery_source')).text,
    _visit = this.getSelected(document.querySelector('#visit')).value;
    //  现在为空也不传
    if (_startdate) { _params['startdate'] = _startdate; }
    if (_enddate) { _params['enddate'] = _enddate; }
    if (_goods !== "无") { _params['goods'] = _goods; }
    if (_source !== "无") { _params['source'] = _source; }
    if (_visit) { _params['visit'] = _visit; }

    _utils.get(_config.creatShortLink, _params, (res) => {
      console.log(res)
      document.getElementById('list_result').innerHTML = this.setTableDate(res);
    })
  },
  bindEvents: function () {
    document.querySelector('#createBtn').addEventListener('click', this.submitCreateForm.bind(this));
  },
  getSelected: function (obj) {
    var _r = obj.options[obj.selectedIndex]
    return {
      text: _r.text,
      value: _r.value
    }
  },
  setTableDate: function (data) {
    /*
        count:3
        created_at:"2018-05-08 00:23:03"
        delivery_source:"FEI_MSG"
        name:"单号-2018050723:40:50"
        product:"CK22"  //  产品
        status:3    //状态
        text:"测试测试{{url}}"
        city:""         //  城市
        hmpl:"123213"     //  渠道描述
        hmsr:"FEI_MSG"    //  渠道来源
        id:445
        net_type:""
        phone:"123123&hmpl"     //  电话
        product:"CK22"    //  产品
        province:""       //省
        short_link:"http://t.tt/7b"
        template:"测试测试{{url}}"
      */
    var _r = "<tr>\
      <th width='100' >id</th>\
      <th width='150' >短链接</th>\
      <th width='100' >手机号</th>\
      <th width='100' >运营商</th>\
      <th width='100' >归属地</th>\
      <th width='100' >产品</th>\
      <th width='100' >渠道来源</th>\
      <th width='100' >渠道描述</th>\
      <th width='100' >访问次数</th>\
      <th width='100' >访问时间</th>\
      <th width='100' >访问设备</th>\
      <th width='100' >浏览器</th>\
      <th width='200' >长链接</th>\
    </tr>";
    data.forEach((e,i) => {
      _r += '<tr>';
      /*for (var j in e ) {
        _r += '<td>' + e[j] + '</td>'
      }*/
      _r += '<td>' + e['id'] + '</td>';
      _r += '<td><a href="http://' +e['short_link'] + '">' + e['short_link'] + '</a></td>';
      _r += '<td>' + e['phone'] + '</td>';
      _r += '<td>' + e['net_type'] +'</td>';
      _r += '<td>' + e['province'] + ' ' + e['city'] + '</td>';
      _r += '<td>' + e['product'] + '</td>';
      _r += '<td>' + e['hmsr'] + '</td>';
      _r += '<td>' + e['hmpl'] + '</td>';
      _r += '<td>' + e['visit_count'] + '</td>';
      _r += '<td colspan=3 class="pd0" >';
      _r += '<table>'
      if (e['visit_info'] && e['visit_info'].length > 0 ) {
        e['visit_info'].forEach(function(item , index) {
          _r += '<tr><td><span class="w80" >' + item['visit_time']
          + '</span></td><td><span class="w80" >' + item['device']
          + '</span></td><td><span class="w80" >' + item['browser_info'] + '</span></td></tr>';
        })
      }
      _r += '</table></td>'
      _r += '<td><a href="' +e['url'] + '">'  + e['url'] + '</a></td>'
      _r += '</tr>';
    })
    return _r
  },
  //  获取初始化数据\
  getInitData: function () {
    _utils.get(_config.getProduct, {
    }, (res) => {
      console.log(res)
      document.querySelector('#product').innerHTML = this.setSelectOptions(res, 'product_name');
    })
    _utils.get(_config.getDeliverySource, {
    }, (res) => {
      console.log(res)
      document.querySelector('#delivery_source').innerHTML = this.setSelectOptions(res, 'delivery_source');
    })
  },
  setSelectOptions: function (arr, key) {
    var _res = '<option value="" n="无" >无</option>';
    //arr = _utils.sortSelect(arr);
    arr.forEach((e, i) => {
      _res += '<option value="' + e['id'] + '" n="' + e[key] + '" >' + e[key] + '</option>'
    })
    return _res
  },
  init: function () {
    this.getInitData();
    this.bindEvents();
  }
}
//  run
_main.init();
